package com.team.teamuserservice.service.impl;

import com.team.teamuserservice.entity.po.BizRescueTask;
import com.team.teamuserservice.service.IBizRescueTaskService;
import com.team.teamuserservice.service.UserCancelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;

import static com.team.teamuserservice.websocket.RescueWebsocketHandler.userSessionMap;

@Service
@RequiredArgsConstructor
public class UserCancelServiceImpl implements UserCancelService {


    private final IBizRescueTaskService bizRescueTaskService;

    /**
     * 用户取消接单
     * @param userId
     * @param merchantId
     * @return
     */
    @Override
    public Boolean cancelRescue(Long userId, Long merchantId) throws IOException {

        //直接修改订单状态
        boolean update = bizRescueTaskService.lambdaUpdate()
                .eq(BizRescueTask::getUserId, userId)
                .eq(BizRescueTask::getStatus, "1")//订单的状态必须是接单中
                .set(BizRescueTask::getStatus, "3")
                .update();
        if (! update){
            //修改失败 , 也是只有一种可能 , 就是商家端也取消了 , 直接return false就行
            return false;
        }

        //通过websocket直接向商家发送老子取消了
        WebSocketSession session = userSessionMap.get(merchantId);
        session.sendMessage(new TextMessage("有个沙雕用户取消了" + userId));

        return true;
    }
}
